﻿using AutoMapper;
using Data0426.Base.Common;
using Data0426.Base.Dommain;
using Data0426.Base.Infrastructure;
using Data0426.Base.Read.API.Applaction.Command;
using MD5Hash;
using MediatR;
using System.Security.Cryptography;

namespace Data0426.Base.Read.API.Applaction.Hander
{
    public class CreateLoginHander : IRequestHandler<CreateLoginCommand, ApiResult<int>>
    {
        private readonly IBaseRequest<Accounts> rAccountsequest;
        private readonly IMapper mapper;

        public CreateLoginHander(IBaseRequest<Accounts> rAccountsequest, IMapper mapper)
        {
            this.rAccountsequest = rAccountsequest;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(CreateLoginCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<int>();
            var entity = mapper.Map<Accounts>(request);
            entity.AccountPassword = entity.AccountPassword.GetMD5();
            var info = rAccountsequest.Create(entity);
            if(info>0)
            {
                result.data = info;
                result.code = ApiEnumcode.OK;
                result.meg = "添加成功";
                return Task.FromResult(result);
            }
            else
            {
                result.data = info;
                result.code = ApiEnumcode.Faile;
                result.meg = "失败";
                return Task.FromResult(result);
            }
        }
    }
}
